package com.itheima.sc.impl.ums;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.sc.api.ums.UmsRoleMenuRelationApi;
import com.itheima.sc.mapper.ums.UmsRoleMenuRelationMapper;
import com.itheima.sc.pojo.ums.UmsRoleMenuRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UmsRoleMenuRelationImpl implements UmsRoleMenuRelationApi {
    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    /**
     * 根据roleId查询role_menu关系表
     */
    @Override
    public List<UmsRoleMenuRelation> roleMenuList(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        return umsRoleMenuRelationMapper.selectList(wrapper);
    }

    /**
     * 先将原始的数据清除
     */
    @Override
    public void deleteByRoleId(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        umsRoleMenuRelationMapper.delete(wrapper);
    }

    /**
     * 给用户分配角色
     */
    @Override
    public void allocMenu(UmsRoleMenuRelation umsRoleMenuRelation) {
        umsRoleMenuRelationMapper.insert(umsRoleMenuRelation);
    }

    /**
     * 根据roleIds查询menus
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<UmsRoleMenuRelation> getByRoleIds(List<Long> roleIds) {
        LambdaQueryWrapper<UmsRoleMenuRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(CollUtil.isNotEmpty(roleIds), UmsRoleMenuRelation::getRoleId, roleIds);
        return umsRoleMenuRelationMapper.selectList(wrapper);
    }
}
